package com.jboltai.util.other;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.StrUtil;
import com.jboltai.util.http.HttpUtil;

/**
 * @author: zhangyi
 * @date: 2024/6/3
 */
public class ImgUtil {

    public static final String BASE64_PREFIX = "data:image/png;base64,";

    /**
     * 将给定的内容转成base64格式图片
     * @param content
     * @return
     */
    public static String parseToBase64(String content) {
        if (StrUtil.isBlank(content)) {
            throw new IllegalArgumentException("传入的参数无效");
        }
        if (Base64.isBase64(content)) {
            return content;
        }
        try {

            byte[] bytes = HttpUtil.get(content).send().bytes();
            return BASE64_PREFIX +  java.util.Base64.getEncoder().encodeToString(bytes);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
}
